<?php

/**
 * Class that seacrh DB for available hotels
 *
 */
class checkout_m extends CI_Model {

     function __construct() {
          parent::__construct();
     }

     public function find_object($object_id) {
          $q = "
            SELECT
            objects.ID as object_ID,
            objects.name as object_name,
            objects.fields as object_fields,
            objects.information as object_info,
            objects.cancellation as object_cancellation,
            destinations.name as destination_name,
            gallery.path as photo_path,
            pictures.filename as photo_name
            FROM objects
            LEFT JOIN object_type ON objects.object_type_ID = object_type.ID
            LEFT JOIN destinations ON objects.destinations_ID = destinations.ID
            LEFT JOIN objects_gallery ON objects.id = objects_gallery.objects_ID
            LEFT JOIN gallery ON objects_gallery.gallery_ID = gallery.ID
            LEFT JOIN pictures ON gallery.ID = pictures.gallery_ID
            AND pictures.ID=
            (
            SELECT pictures.ID
            FROM pictures
            WHERE pictures.gallery_ID = gallery.ID
            ORDER BY pictures.order ASC, pictures.ID ASC
            LIMIT 0, 1  
            )
            WHERE objects.ID = ?";

          $res = $this->db->query($q, array($object_id))->row_array();

          return $res;
     }

     public function find_room($room_id) {

          $q = "
            SELECT
            objects.ID as object_ID,
            objects.name as object_name,
            gallery.path as photo_path,
            pictures.filename as photo_name,
            service_type.name AS service_name,
            room_type.name AS room_type_name,
            rooms.ID AS room_ID,
            rooms.description AS room_description,
            rooms.information AS room_info,
            rooms.cancellation AS room_cancellation,
            rooms.max_person AS room_max_person,
            rooms.free_storno AS room_free_storno,
            rooms.units_left AS room_units_left
            FROM objects
            RIGHT JOIN rooms ON rooms.objects_ID = objects.ID
            LEFT JOIN room_type on rooms.room_type_ID = room_type.ID
            LEFT JOIN service_type on rooms.service_type_ID = service_type.ID
            LEFT JOIN rooms_has_gallery ON rooms.ID = rooms_has_gallery.rooms_ID
            LEFT JOIN gallery ON rooms_has_gallery.gallery_ID = gallery.ID
            LEFT JOIN pictures ON gallery.ID = pictures.gallery_ID
            AND pictures.ID=
            (
            SELECT pictures.ID
            FROM pictures
            WHERE pictures.gallery_ID = gallery.ID
            ORDER BY pictures.order ASC, pictures.ID ASC
            LIMIT 0, 1  
            )

            WHERE rooms.ID = ?
            
            ";


          $res = $this->db->query($q, array($room_id))->row_array();

          //log_message("error", $this->db->last_query());
          //log_message("error", $object_type);

          return $res;
     }

}

?>
